package com.koo.modules.purchase.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.koo.common.utils.MyStrUtil;
import com.koo.common.utils.PageUtils;
import com.koo.common.utils.Query;
import com.koo.modules.purchase.dao.PurchaseOrderDetailDao;
import com.koo.modules.purchase.entity.PurchaseOrderDetailEntity;
import com.koo.modules.purchase.entity.PurchaseOrderEntity;
import com.koo.modules.purchase.service.PurchaseOrderDetailService;
import com.koo.modules.purchase.service.PurchaseOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class PurchaseOrderDetailServiceImpl extends ServiceImpl<PurchaseOrderDetailDao, PurchaseOrderDetailEntity> implements PurchaseOrderDetailService {

    @Autowired
    private PurchaseOrderService purchaseOrderService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {

        Object key = params.get("key");
        LambdaQueryWrapper<PurchaseOrderDetailEntity> queryWrapper = new LambdaQueryWrapper<>();
//        queryWrapper.like(MyStrUtil.isNotEmpty(key), PurchaseOrderDetailEntity::getName, key)
//                  .or().eq(MyStrUtil.isNotEmpty(key), PurchaseOrderDetailEntity::getCode, key);

        IPage<PurchaseOrderDetailEntity> page = this.page(
                new Query<PurchaseOrderDetailEntity>().getPage(params),
                queryWrapper
        );

        return new PageUtils(page);
    }

    @Override
    public List<PurchaseOrderDetailEntity> listByStoreId(Long storeId) {
        LambdaQueryWrapper<PurchaseOrderDetailEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(MyStrUtil.isNotEmpty(storeId), PurchaseOrderDetailEntity::getStoreId, storeId);
        return this.list(wrapper);
    }

    @Override
    public List<PurchaseOrderDetailEntity> listByPurchaseOrderId(String purchaseOrderId) {
        return baseMapper.selectList(new LambdaQueryWrapper<PurchaseOrderDetailEntity>().eq(PurchaseOrderDetailEntity::getPurchaseOrderId, purchaseOrderId));
    }

    /**
     * 1、更新单据总价
     * 2、各个明细
     *
     * @param purchaseOrderDetail
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(PurchaseOrderDetailEntity purchaseOrderDetail) {
        purchaseOrderDetail.setPurchaseFee(purchaseOrderDetail.getPurchaseCount().multiply(purchaseOrderDetail.getPurchasePrice()));
        baseMapper.updateById(purchaseOrderDetail);

        Long purchaseOrderId = purchaseOrderDetail.getPurchaseOrderId();
        List<PurchaseOrderDetailEntity> detailList = baseMapper.selectList(new LambdaQueryWrapper<PurchaseOrderDetailEntity>().eq(PurchaseOrderDetailEntity::getPurchaseOrderId, purchaseOrderId));
        PurchaseOrderEntity orderEntity = purchaseOrderService.info(purchaseOrderId.toString());
        BigDecimal totalFee = detailList.stream().map(p->p.getPurchaseFee()).reduce(BigDecimal.ZERO, BigDecimal::add);
        orderEntity.setTotalFee(totalFee);
        boolean updateOrder = purchaseOrderService.updateById(orderEntity);
        log.info("update updateOrder==" + updateOrder);
    }

    @Override
    public void delete(String purchaseOrderDetaiId, String purchaseOrderId) {
        baseMapper.deleteById(purchaseOrderDetaiId);

        List<PurchaseOrderDetailEntity> detailList = baseMapper.selectList(new LambdaQueryWrapper<PurchaseOrderDetailEntity>().eq(PurchaseOrderDetailEntity::getPurchaseOrderId, purchaseOrderId));
        PurchaseOrderEntity orderEntity = purchaseOrderService.info(purchaseOrderId.toString());
        BigDecimal totalFee = detailList.stream().map(p->p.getPurchaseFee()).reduce(BigDecimal.ZERO, BigDecimal::add);
        orderEntity.setTotalFee(totalFee);
        boolean updateOrder = purchaseOrderService.updateById(orderEntity);
        log.info("delete updateOrder==" + updateOrder);
    }
}